using System.Collections.Generic;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Webshop20.Constants;

namespace Webshop20.AntiCorruptionLayers.Lucene
{
    class ProductFullTextSearchFacade : LuceneFacadeBase, IProductFullTextSearchFacade
    {
        public IEnumerable<Document> Search(string searchTerm)
        {
            var indexReader = IndexReader.Open(dir);
            var indexSearcher = new IndexSearcher(indexReader);

            var queryParser = new QueryParser(CONSTANTS.KEYS.FULLTEXT_SEARCH_CONTENT_FIELD, analyzer);
            var query = queryParser.Parse(searchTerm);
            var hits = indexSearcher.Search(query);

            for (int i = 0; i < hits.Length(); i++)
            {
                yield return hits.Doc(i);
            }
        }
    }
}